package com.qf.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.dto.ReportDTO;
import com.qf.hospital.entity.Report;
import com.qf.hospital.entity.ReportExample;
import com.qf.hospital.enums.HospitalErrorEnums;
import com.qf.hospital.exception.HospitalException;
import com.qf.hospital.mapper.ReportMapper;
import com.qf.hospital.service.ReportService;
import com.qf.hospital.vo.ReportVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.service.impl
 * @Description: ${TODO}
 * @Date 2021/10/19 18:42
 */

@Service
public class ReportServiceImpl implements ReportService {

    private Logger log = LoggerFactory.getLogger(ReportServiceImpl.class);

    //已删除
    private final int DELETE = 0;
    //预约成功
    private final int SUCCESS = 1;
    //药品缴费成功
    private final int DRUG_PAY_SUCCESS = 2;
    //药品出库成功
    private final int DRUG_EX = 3;
    //转住院
    private final int HOSPITALIZATION = 4;

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public Integer findReportCountTotal() {
        //1.封装查询条件
        ReportExample example = new ReportExample();
        ReportExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(DELETE);

        //2.查询
        long count = reportMapper.countByExample(example);

        return Integer.valueOf(count + "");
    }

    @Override
    public Integer findReportCountCurrent() {
        //.查询
        long count = reportMapper.findReportCountByCurrent();

        return Integer.valueOf(count + "");
    }

    @Override
    public List<ReportVO> findPatientInfo(Integer cc, String name) {
        List<Report> reportList = reportMapper.findPatientInformation(cc,name);

        //封装数据
        List<ReportVO> data = new ArrayList<>();
        for (Report report : reportList) {
            ReportVO vo = new ReportVO();
            BeanUtils.copyProperties(report, vo);
            data.add(vo);
        }
        return data;
    }

    @Override
    public Integer findPatientByPhoneAndCarid(String phone, String carid) {
        int count = reportMapper.checkExist("phone",phone);
        if (count >= 1){
            return 1;
        }
        count = reportMapper.checkExist("carid",carid);

        return count == 1? 2 : 3;
    }

    @Override
    public void addReport(ReportDTO reportDTO) {
        Report report = new Report();
        BeanUtils.copyProperties(reportDTO, report);
        report.setDepartment(reportDTO.getDepartmentId());
        report.setReportType(reportDTO.getRegisterTypeId());


        report.setDoctor(reportDTO.getDoctorId());

        //封装state和time
        report.setTime(new Date());
        report.setState(SUCCESS);

        int count = reportMapper.insertSelective(report);

        if (count != 1){
            log.error("【添加患者】 添加患者失败 report={}", report);
            throw new HospitalException(HospitalErrorEnums.REPORT_ADD_ERROR);
        }
    }

    @Override
    public PageInfo<Report> findPresentionInfo(String name, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);

        List<Report> result = reportMapper.findPresentionInfo(name);

        PageInfo<Report> pageInfo = new PageInfo<>(result);

        return pageInfo;
    }

    @Override
    public String findSickReason(Integer reid) {
         return reportMapper.fingZhuanById(reid);
    }

    @Override
    public Report findById(Integer reid) {
        return reportMapper.selectByPrimaryKey(reid);
    }

    @Override
    public PageInfo<Report> findPatientByDrugName(String durgname, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);

        List<Report> result = reportMapper.findPatientByDrugName(durgname);

        PageInfo<Report> pageInfo = new PageInfo<>(result);

        return pageInfo;
    }
}
